package indi.lqzhi.basic.ui.activity.general;

import android.annotation.SuppressLint;
import android.view.MenuInflater;
import android.view.MenuItem;

import androidx.annotation.MenuRes;
import androidx.appcompat.view.menu.MenuBuilder;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import indi.lqzhi.basic.R;
import indi.lqzhi.basic.databinding.ActivityMainViewpageBinding;
import indi.lqzhi.basic.tools.Tools;
import indi.lqzhi.basic.ui.basic.BaseActivity;
import indi.lqzhi.basic.ui.basic.adapter.ViewPage2FragmentStatusAdapter;

public abstract class ViewPageMainActivity extends BaseActivity<ActivityMainViewpageBinding> {

    /**
     * 底部导航菜单
     */
    protected BottomNavigationView bottomNavigationView;

    /**
     * viewpage的对象
     */
    protected ViewPager2 viewPager;

    /**
     * 适配器
     */
    protected ViewPage2FragmentStatusAdapter adapter;

    /**
     * fragment的列表
     */
    protected List<Fragment> fragments;

    /**
     * 菜单的列表
     */
    protected ArrayList<? extends MenuItem> menuItems;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main_viewpage;
    }

    @Override
    protected void initAcivity() {
        bottomNavigationView = bindView.bottomNavigationView;
        viewPager = bindView.viewpage;
        initCheck();
        initViewPage();
        initBottomNavigationView();
        initThisAcivity();//后续的调用
    }

    /**
     * 初始化ViewPage
     */
    protected void initViewPage(){
        adapter = new ViewPage2FragmentStatusAdapter(ViewPageMainActivity.this, fragments);
        viewPager.setAdapter(adapter);
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                d("这是第"+(position % menuItems.size())+"个页面");
                bottomNavigationView.setSelectedItemId(menuItems.get(position % menuItems.size()).getItemId());
            }
        });
    }

    /**
     * 初始化BottomNavigationView
     */

    protected void initBottomNavigationView(){
        //设置将BottomNavigationView
        bottomNavigationView.inflateMenu(getMenus());
        //设置BottomNavigationView菜单点击跳转
        bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            //从menuItem中找到id相同的
            int index = 0 ;
            for (MenuItem menuItem : menuItems) {
                if (menuItem.getItemId() == item.getItemId()) {
                    break;
                }
                index++;
            }
            viewPager.setCurrentItem(index);
            return true;
        });
    }

    /**
     * 初始化检查
     */
    @SuppressLint("RestrictedApi")
    private void initCheck(){
        fragments = Tools.checkNull(getFragments(), new NullPointerException("Fragment列表不能为空！"));
        int menus = getMenus();
        if (menus <= 0){
            throw new UnsupportedOperationException("未指定菜单资源!");
        }
        MenuInflater inflater=new MenuInflater(this); //实例化一个MenuInflater对象
        MenuBuilder menu = new MenuBuilder(this);
        inflater.inflate(menus,menu);
        menuItems = menu.getVisibleItems();
        if (fragments.size() != menuItems.size()){
            //fragment的数量和菜单的数量不对等！
            throw new UnsupportedOperationException("fragment的数量和菜单的数量不对等！");
        }
    }

    /**
     * 返回匹配的Fragments
     * @return fragments
     */
    @NotNull
    public abstract List<Fragment> getFragments();

    /**
     * 返回匹配的菜单资源
     * @return 菜单的menus
     */
    @MenuRes
    public abstract int getMenus();

    /**
     * 初始化当前的activity
     */
    protected abstract void initThisAcivity();

}
